from pyprotobuf.compiler import Compiler, CompilerInput
import pyprotobuf.nodes as nodes
import os
import unittest


class CompilerTestCase(unittest.TestCase):
    def test_single_file_compile(self):
        """
            Tests message type resolution between files.
        """
        input = CompilerInput('pyprotobuf/tests/a.proto')

        c = Compiler()
        c.set_inputs([input])
        root_node = c.compile()

        self.assertIsInstance(root_node, nodes.RootNode)

        # XXX: we are actually testing the name resolver here

        self.assertEqual(2, len(root_node.get_children()))

        # check the children are Package
        self.assertIsInstance(root_node.get_child(0), nodes.Package)
        self.assertIsInstance(root_node.get_child(1), nodes.Package)


        file_b = root_node.get_child(0).get_child(0)
        file_a = root_node.get_child(1).get_child(0)
        
        # check that imports resolve in the right order
        self.assertEquals('b.proto', os.path.basename(file_b.filename))
        self.assertEquals('a.proto', os.path.basename(file_a.filename))
        
        
        
        message_B = file_b.get_child(1)
        message_A = file_a.get_child(1)
        
        self.assertIsInstance(message_B, nodes.MessageNode)
        self.assertIsInstance(message_A, nodes.MessageNode)

        self.assertEquals(0, len(message_B.get_children()))
        self.assertEquals(1, len(message_A.get_children()))

        self.assertIsInstance(message_A.get_child(0).type, nodes.MessageNode)
        
        
        # check message A field's type resolves
        self.assertEqual(message_A.get_child(0).type, message_B)


if __name__ == '__main__':
    unittest.main()
